package cn.ffcs.bpmpvc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.echoice.hibernate.HibernateEntityDao;
import org.echoice.paper.PageBean;

import cn.ffcs.bpmpvc.dao.BusinessAnalysisDao;
import cn.ffcs.bpmpvc.dao.mapper.BusinessAnalysisMapper;
import cn.ffcs.bpmpvc.model.BusinessAnalysis;

public class BusinessAnalysisDaoImpl extends HibernateEntityDao<BusinessAnalysis> implements BusinessAnalysisDao{
	public PageBean searchBusinessAnalysPage(BusinessAnalysis searchForm,int pageNo,int pageSize){
		StringBuilder sql=new StringBuilder();
		sql.append("select t1.app_id,t1.issuse,t1.op_user,t2.name op_user_cnname,t1.org_id,t3.short_name,t1.op_time,t3.oa_full_name");
		sql.append(" from t_app_business_analysis t1,t_organ_person t2,t_organ_organization t3");
		sql.append(" where t1.op_user=t2.id and t1.org_id=t3.id");
		List paramList=new ArrayList();
		if(StringUtils.isNotBlank(searchForm.getIssuse())){
			sql.append(" and t1.issuse=?");
			paramList.add(searchForm.getIssuse());
		}
		
		if(StringUtils.isNotBlank(searchForm.getOrgCnName())){
			sql.append(" and t3.short_name=?");
			paramList.add(searchForm.getOrgCnName());
		}
		sql.append(" order by t1.op_time desc");
		return super.pagedQuerySQLJdbcTemplate(sql.toString(), new BusinessAnalysisMapper(), pageNo, pageSize, paramList.toArray());
	}
	
	public BusinessAnalysis getBusinessAnalysis(String appId){
		StringBuilder sql=new StringBuilder();
		sql.append("select t1.app_id,t1.issuse,t1.op_user,t2.name op_user_cnname,t1.org_id,t3.short_name,t1.op_time,t3.oa_full_name");
		sql.append(" from t_app_business_analysis t1,t_organ_person t2,t_organ_organization t3");
		sql.append(" where t1.op_user=t2.id and t1.org_id=t3.id and t1.app_id=?");
		List list=getJdbcTemplate().query(sql.toString(), new Object[]{appId}, new BusinessAnalysisMapper());
		if(list!=null&&list.size()>0){
			return (BusinessAnalysis)list.get(0);
		}
		return null;
	}
	
	public void delBusinessAnalysis(String appId){
		String sql="delete from t_app_business_analysis t where t.app_id=?";
		getJdbcTemplate().update(sql, new Object[]{appId});
	}
	
}
